const cloud = require('wx-server-sdk'); 
 
cloud.init({ 
  env: cloud.DYNAMIC_CURRENT_ENV 
}); 
const db = cloud.database(); 
 
exports.main = async (event, context) => { 
  const wxContext = cloud.getWXContext() 
 
  const user = await db.collection("user").where({ 
    openid:wxContext.OPENID, 
  }).get() 
 
  if(user.data.length == 0) 
  { 
    return { 
      code:401, 
      data:{}, 
      msg:"have not login" 
   } 
  } 
   
  const result = await db.collection("order").where({ 
    sid:user.data[0].sid, 
  }).orderBy("timestamp","desc").limit(30).field({ 
    timestamp:true, 
    state:true, 
    judge_person:true, 
    fail_reason:true,     
  }).get() 
  if(result.data.length == 0) 
  { 
    return { 
      code:404, 
      data:{}, 
      msg:"empty result" 
    } 
  } 
  return { 
    code:200, 
    data:result.data, 
    msg:"success" 
  } 
}; 